package com.longcai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.longcai.bean.ApiResult;
import com.longcai.bean.PageHelper;
import com.longcai.entity.auth.ClyjShopsArea;
import com.longcai.entity.customer.ClyjUser;
import com.longcai.entity.homepage.GoodsClass;
import com.longcai.entity.nearby.ClyjNearbyGoodsType;
import com.longcai.entity.nearby.ClyjNearbyShops;
import com.longcai.entity.nearby.ClyjNearbyShopsGoods;
import com.longcai.mapper.ClyjNearbyShopsMapper;
import com.longcai.mapper.GoodsClassMapper;
import com.longcai.mapper.clyjuser.ClyjShopsAreaMapper;
import com.longcai.mapper.nearbyshop.ClyjNearbyGoodsTypeMapper;
import com.longcai.page.table.PageTableResponse;
import com.longcai.service.ClyjNearbyShopsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longcai.util.LoginUser;
import com.longcai.util.UserUtil;
import com.longcai.util.listUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.slf4j.Logger;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 附近商铺 服务实现类
 * </p>
 *
 * @author ZZN
 * @since 2019-04-11
 */
@Service
public class ClyjNearbyShopsServiceImpl extends ServiceImpl<ClyjNearbyShopsMapper, ClyjNearbyShops> implements ClyjNearbyShopsService {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ClyjNearbyShopsMapper clyjNearbyShopsMapper;
    @Autowired
    private ClyjShopsAreaMapper clyjShopsAreaMapper;
    @Autowired
    private GoodsClassMapper goodsClassMapper;

    /**
     * 获取店铺信息
     *
     * @param clyjNearbyShops
     * @return
     */
    @Override
    public ApiResult shopsList(ClyjNearbyShops clyjNearbyShops) {
        try {
            //查询登录用户角色
            LoginUser loginUser = UserUtil.getLoginUser();
            //查询店铺
            QueryWrapper<ClyjNearbyShops> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status", 0);
            queryWrapper.eq("mich_id", loginUser.getUserId());
            ClyjNearbyShops clyjNearbyShops1 = this.clyjNearbyShopsMapper.selectOne(queryWrapper);
            GoodsClass goodsClass = this.goodsClassMapper.selectById(clyjNearbyShops1.getShopsType());
            clyjNearbyShops1.setShopsType(goodsClass.getName());
            logger.debug("查询成功");
            return ApiResult.success(clyjNearbyShops1);
        } catch (Exception e) {
            logger.error("查询失败");
            e.printStackTrace();
            return ApiResult.fail("查询失败" + e.getMessage());
        }
    }


    /**
     * 根据Id获取店铺信息
     *
     * @param id
     * @return
     */
    @Override
    public ApiResult getById(String id) {
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("shops_id", id);
            ClyjNearbyShops clyjNearbyShops = this.clyjNearbyShopsMapper.selectOne(queryWrapper);
            logger.debug("查询成功", clyjNearbyShops);
            return ApiResult.success(clyjNearbyShops);
        } catch (Exception e) {
            logger.error("查询失败");
            e.printStackTrace();
            return ApiResult.fail("查询失败" + e.getMessage());
        }
    }

    /**
     * 删除店铺
     *
     * @param id
     */
    @Override
    public void deleteShopsList(String id) {
        try {
            QueryWrapper<ClyjNearbyShops> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("shops_id", id);
            ClyjNearbyShops clyjNearbyShops = new ClyjNearbyShops();
            clyjNearbyShops.setStatus("1");
            clyjNearbyShopsMapper.update(clyjNearbyShops, queryWrapper);
        } catch (Exception e) {
            e.getMessage();
            e.printStackTrace();
        }

    }

    /**
     * 获取所有店铺名称
     *
     * @return
     */
    @Override
    public List<ClyjNearbyShops> getAllshops() {
        return clyjNearbyShopsMapper.getAllshops();
    }

    /**
     * 修改店铺信息
     *
     * @param clyjNearbyShops
     * @return
     */
    @Override
    public ApiResult updateShopsList(ClyjNearbyShops clyjNearbyShops) {
        try {
            QueryWrapper<ClyjNearbyShops> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("shops_id", clyjNearbyShops.getShopsId());
            Integer integer = this.clyjNearbyShopsMapper.update(clyjNearbyShops, queryWrapper);
            logger.debug("修改成功");
            return ApiResult.success(integer);
        } catch (Exception e) {
            logger.error("修改失败");
            e.printStackTrace();
            return ApiResult.fail("修改失败" + e.getMessage());
        }
    }
}
